#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>

class InetAddr
{
private:
    void PortNet2Host()
    {
        _prot = ::ntohs(_addr.sin_port);
    }

    void IpNet2Host()
    {
        char ipbuffer[64];
        const char *ip = ::inet_ntop(AF_INET, &_addr.sin_addr, ipbuffer, sizeof(ipbuffer));
        _ip = ip;

    }

public:
    InetAddr() = default;
    InetAddr(uint16_t prot)
        : _prot(prot),
          _ip("")
    {
        //::bzero(&_addr,sizeof(_addr));
        _addr.sin_family = AF_INET;
        _addr.sin_port = ::htons(_prot);
        _addr.sin_addr.s_addr = INADDR_ANY;
    }

    InetAddr(const struct sockaddr_in &addr) : _addr(addr)
    {
        PortNet2Host();
        IpNet2Host();
    }

    socklen_t Addr_size()
    {
        return sizeof(_addr);
    }

    uint16_t GetProt()
    {
        return _prot;
    }

    std::string GetIp() const 
    {
        return _ip;
    }

    struct sockaddr * GetAddr()
    {
        return (struct sockaddr *) &_addr;
    }

    bool operator==(const InetAddr& addr)
    {
        return _ip == addr._ip;
    }

    ~InetAddr() = default;

private:
    uint16_t _prot;
    std::string _ip;
    struct sockaddr_in _addr;
};